<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>沃林学生信息管理系统</title>
    <style>
        /* 全局样式 */
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        
        body {
            background-color: #f5f5f5;
            color: #333;
            line-height: 1.6;
        }
        
        .container {
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
        }
        
        header {
            background-color: #2c3e50;
            color: white;
            padding: 20px 0;
            text-align: center;
            margin-bottom: 30px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
        }
        
        .subtitle {
            font-size: 1.2rem;
            font-weight: 300;
        }
        
        /* 导航栏样式 */
        nav {
            background-color: #3498db;
            border-radius: 5px;
            margin-bottom: 30px;
            overflow: hidden;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        nav ul {
            display: flex;
            list-style: none;
        }
        
        nav li {
            flex: 1;
            text-align: center;
        }
        
        nav a {
            display: block;
            padding: 15px 0;
            color: white;
            text-decoration: none;
            transition: background-color 0.3s;
        }
        
        nav a:hover, nav a.active {
            background-color: #2980b9;
        }
        
        /* 内容区域样式 */
        .content {
            background-color: white;
            border-radius: 5px;
            padding: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            margin-bottom: 30px;
        }
        
        .tab-content {
            display: none;
        }
        
        .tab-content.active {
            display: block;
        }
        
        /* 表单样式 */
        .form-group {
            margin-bottom: 15px;
        }
        
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        
        input, select, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        
        button {
            background-color: #3498db;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }
        
        button:hover {
            background-color: #2980b9;
        }
        
        /* 表格样式 */
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        
        /* 统计详情容器样式 */
        #statistics-detail-container {
            max-height: 500px;
            overflow-y: auto;
        }
        
        #statistics-detail-content {
            overflow-x: auto;
        }
        
        th, td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid #ddd;
        }
        
        th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        
        tr:hover {
            background-color: #f5f5f5;
        }
        
        /* 卡片样式 */
        .card {
            background-color: white;
            border-radius: 5px;
            padding: 20px;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        
        .card h3 {
            margin-bottom: 15px;
            color: #2c3e50;
        }
        
        /* 网格布局 */
        .grid {
            display: grid;
            grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
            gap: 20px;
        }
        
        /* 响应式设计 */
        @media (max-width: 768px) {
            nav ul {
                flex-direction: column;
            }
            
            .grid {
                grid-template-columns: 1fr;
            }
        }
        
        /* 消息提示样式 */
        .message {
            padding: 10px 15px;
            margin-bottom: 20px;
            border-radius: 4px;
        }
        
        .message.success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        
        .message.error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        
        .message.info {
            background-color: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }
        
        /* 按钮组样式 */
        .btn-group {
            display: flex;
            gap: 10px;
            margin-bottom: 20px;
        }
        
        .btn-group button {
            flex: 1;
        }
        
        /* 搜索区域样式 */
        .search-area {
            background-color: #f8f9fa;
            padding: 15px;
            border-radius: 4px;
            margin-bottom: 20px;
        }
        
        .search-area h3 {
            margin-bottom: 15px;
            color: #2c3e50;
        }
        
        /* 统计卡片样式 */
        .stat-card {
            background-color: white;
            border-radius: 5px;
            padding: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
            text-align: center;
            transition: transform 0.3s;
        }
        
        .stat-card:hover {
            transform: translateY(-5px);
        }
        
        .stat-card h3 {
            color: #2c3e50;
            margin-bottom: 10px;
        }
        
        .stat-card .value {
            font-size: 2rem;
            font-weight: bold;
            color: #3498db;
            margin-bottom: 10px;
        }
        
        .stat-card .description {
            color: #7f8c8d;
        }
        
        /* 输入对话框样式 */
        .input-dialog {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background-color: rgba(0, 0, 0, 0.5);
            display: flex;
            justify-content: center;
            align-items: center;
            z-index: 1000;
        }
        
        .input-dialog-content {
            background-color: white;
            padding: 20px;
            border-radius: 5px;
            width: 300px;
            box-shadow: 0 0 10px rgba(0,0,0,0.3);
        }
        
        .input-dialog-content h3 {
            margin-top: 0;
            margin-bottom: 15px;
            text-align: center;
            color: #2c3e50;
        }
        
        .input-dialog-content input {
            margin-bottom: 15px;
        }
        
        .dialog-buttons {
            display: flex;
            justify-content: space-between;
        }
        
        .dialog-buttons button {
            width: 45%;
        }
        
        #cancelClassId {
            background-color: #e74c3c;
        }
        
        #cancelClassId:hover {
            background-color: #c0392b;
        }
    </style>
</head>
<body>
    <header>
        <div class="container">
            <h1>沃林学生信息管理系统</h1>
            <p class="subtitle">对所有的学员，学习过程，班级，成绩进行管理</p>
        </div>
    </header>
    
    <div class="container">
        <nav>
            <ul>
                <li><a href="#" class="nav-link active" data-tab="student-tab">学生信息管理</a></li>
                <li><a href="#" class="nav-link" data-tab="score-tab">成绩管理</a></li>
                <li><a href="#" class="nav-link" data-tab="employment-tab">就业管理</a></li>
                <li><a href="#" class="nav-link" data-tab="statistics-tab">统计分析</a></li>
            </ul>
        </nav>
        
        <div id="message-container"></div>
        
        <!-- 学生信息管理标签页 -->
        <div id="student-tab" class="tab-content active">
            <div class="content">
                <h2>学生信息管理</h2>
                
                <div class="btn-group">
                    <button id="add-student-btn">添加学生</button>
                    <button id="show-all-students-btn">显示所有学生</button>
                </div>
                
                <!-- 添加学生表单 -->
                <div id="add-student-form" class="card" style="display: none;">
                    <h3>添加学生</h3>
                    <form id="student-form">
                        <div class="form-group">
                            <label for="class-id">班级编号</label>
                            <input type="text" id="class-id" name="class_id" required>
                        </div>
                        <div class="form-group">
                            <label for="student-name">学生姓名</label>
                            <input type="text" id="student-name" name="student_name" required>
                        </div>
                        <div class="form-group">
                            <label for="native-place">籍贯</label>
                            <input type="text" id="native-place" name="native_place" required>
                        </div>
                        <div class="form-group">
                            <label for="graduate-school">毕业院校</label>
                            <input type="text" id="graduate-school" name="graduate_school" required>
                        </div>
                        <div class="form-group">
                            <label for="major">专业</label>
                            <input type="text" id="major" name="major" required>
                        </div>
                        <div class="form-group">
                            <label for="enrollment-date">入学时间</label>
                            <input type="date" id="enrollment-date" name="enrollment_date" required>
                        </div>
                        <div class="form-group">
                            <label for="graduation-date">毕业时间</label>
                            <input type="date" id="graduation-date" name="graduation_date" required>
                        </div>
                        <div class="form-group">
                            <label for="education">学历</label>
                            <select id="education" name="education" required>
                                <option value="本科">本科</option>
                                <option value="硕士">硕士</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <label for="advisor-id">顾问编号</label>
                            <input type="text" id="advisor-id" name="advisor_id" required>
                        </div>
                        <div class="form-group">
                            <label for="age">年龄</label>
                            <input type="number" id="age" name="age" min="0" max="100" required>
                        </div>
                        <div class="form-group">
                            <label for="gender">性别</label>
                            <select id="gender" name="gender" required>
                                <option value="男">男</option>
                                <option value="女">女</option>
                            </select>
                        </div>
                        <button type="submit">提交</button>
                    </form>
                </div>
                
                <!-- 搜索学生 -->
                <div class="search-area">
                    <h3>搜索学生</h3>
                    <div class="form-group">
                        <label for="search-student-name">按学生姓名搜索</label>
                        <input type="text" id="search-student-name" placeholder="输入学生姓名">
                        <button id="search-student-btn">搜索</button>
                    </div>
                </div>
                
                <!-- 学生列表 -->
                <div id="student-list-container">
                    <h3>学生列表</h3>
                    <div id="student-table-container">
                        <p>请点击"显示所有学生"按钮查看学生列表</p>
                    </div>
                </div>
                
                <!-- 班级管理 -->
                <div class="card">
                    <h3>班级管理</h3>
                    <div class="btn-group">
                        <button id="show-all-classes-btn">显示所有班级</button>
                        <button id="add-class-btn">添加班级</button>
                    </div>
                    
                    <!-- 添加班级表单 -->
                    <div id="add-class-form" style="display: none;">
                        <h4>添加班级</h4>
                        <form id="class-form">
                            <div class="form-group">
                            <label for="new-class-id">班级编号</label>
                            <input type="text" id="new-class-id" name="class_id" required>
                            <small>班级编号必须是6个字符</small>
                        </div>
                            <div class="form-group">
                                <label for="new-class-name">班级名称</label>
                                <input type="text" id="new-class-name" name="class_name" required>
                            </div>
                            <button type="submit">提交</button>
                        </form>
                    </div>
                    
                    <!-- 班级列表 -->
                    <div id="class-list-container">
                        <h4>班级列表</h4>
                        <div id="class-table-container">
                            <p>请点击"显示所有班级"按钮查看班级列表</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 成绩管理标签页 -->
        <div id="score-tab" class="tab-content">
            <div class="content">
                <h2>成绩管理</h2>
                
                <div class="btn-group">
                    <button id="add-score-btn">添加成绩</button>
                    <button id="show-all-scores-btn">显示所有成绩</button>
                </div>
                
                <!-- 添加成绩表单 -->
                <div id="add-score-form" class="card" style="display: none;">
                    <h3>添加成绩</h3>
                    <form id="score-form">
                        <div class="form-group">
                            <label for="score-student-id">学生编号</label>
                            <input type="number" id="score-student-id" name="student_id" required>
                        </div>
                        <div class="form-group">
                            <label for="score-round">考核序次</label>
                            <input type="number" id="score-round" name="round" required>
                        </div>
                        <div class="form-group">
                            <label for="score-value">成绩</label>
                            <input type="number" id="score-value" name="score" min="0" max="100" required>
                        </div>
                        <button type="submit">提交</button>
                    </form>
                </div>
                
                <!-- 搜索成绩 -->
                <div class="search-area">
                    <h3>搜索成绩</h3>
                    <div class="form-group">
                        <label for="search-score-name">按学生姓名搜索</label>
                        <input type="text" id="search-score-name" placeholder="输入学生姓名">
                        <button id="search-score-btn">搜索</button>
                    </div>
                </div>
                
                <!-- 成绩列表 -->
                <div id="score-list-container">
                    <h3>成绩列表</h3>
                    <div id="score-table-container">
                        <p>请点击"显示所有成绩"按钮查看成绩列表</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 就业管理标签页 -->
        <div id="employment-tab" class="tab-content">
            <div class="content">
                <h2>就业管理</h2>
                
                <div class="btn-group">
                    <button id="add-employment-btn">添加就业信息</button>
                    <button id="show-all-employments-btn">显示所有就业信息</button>
                </div>
                
                <!-- 添加就业信息表单 -->
                <div id="add-employment-form" class="card" style="display: none;">
                    <h3>添加就业信息</h3>
                    <form id="employment-form">
                        <div class="form-group">
                            <label for="employment-student-id">学生编号</label>
                            <input type="number" id="employment-student-id" name="student_id" required>
                        </div>
                        <div class="form-group">
                            <label for="open-time">就业开放时间</label>
                            <input type="date" id="open-time" name="open_time" required>
                        </div>
                        <div class="form-group">
                            <label for="offer-time">Offer下发时间</label>
                            <input type="date" id="offer-time" name="offer_time" required>
                        </div>
                        <div class="form-group">
                            <label for="company">公司名称</label>
                            <input type="text" id="company" name="company" required>
                        </div>
                        <div class="form-group">
                            <label for="salary">就业薪资</label>
                            <input type="number" id="salary" name="salary" min="0" required>
                        </div>
                        <button type="submit">提交</button>
                    </form>
                </div>
                
                <!-- 搜索就业信息 -->
                <div class="search-area">
                    <h3>搜索就业信息</h3>
                    <form id="search-employment-form">
                        <div class="form-group">
                            <label for="search-employment-student-id">学生编号</label>
                            <input type="number" id="search-employment-student-id" name="stu_id">
                        </div>
                        <div class="form-group">
                            <label for="search-company-name">公司名称</label>
                            <input type="text" id="search-company-name" name="comp_name">
                        </div>
                        <div class="form-group">
                            <label for="min-salary">最低薪资</label>
                            <input type="number" id="min-salary" name="min_sal" min="0">
                        </div>
                        <div class="form-group">
                            <label for="max-salary">最高薪资</label>
                            <input type="number" id="max-salary" name="max_sal" min="0">
                        </div>
                        <button type="submit">搜索</button>
                    </form>
                </div>
                
                <!-- 就业信息列表 -->
                <div id="employment-list-container">
                    <h3>就业信息列表</h3>
                    <div id="employment-table-container">
                        <p>请点击"显示所有就业信息"按钮查看就业信息列表</p>
                    </div>
                </div>
            </div>
        </div>
        
        <!-- 统计分析标签页 -->
        <div id="statistics-tab" class="tab-content">
            <div class="content">
                <h2>统计分析</h2>
                
                <div class="grid">
                    <div class="stat-card">
                        <h3>超过30岁的学员</h3>
                        <div class="value" id="age-count">-</div>
                        <div class="description">系统中年龄超过30岁的学员数量</div>
                        <button id="show-age-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>班级性别统计</h3>
                        <div class="value" id="gender-count">-</div>
                        <div class="description">统计指定班级的男生女生人数</div>
                        <button id="show-gender-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>高成绩学生</h3>
                        <div class="value" id="high-score-count">-</div>
                        <div class="description">每次考试成绩都在80分以上的学生</div>
                        <button id="show-high-score-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>不及格学生</h3>
                        <div class="value" id="fail-count">-</div>
                        <div class="description">有两次以上不及格的学生</div>
                        <button id="show-fail-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>班级平均分</h3>
                        <div class="value" id="class-avg-score">-</div>
                        <div class="description">每次考试每个班级的平均分</div>
                        <button id="show-class-avg-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>高薪学生</h3>
                        <div class="value" id="high-salary-count">-</div>
                        <div class="description">就业薪资最高的前五名学生</div>
                        <button id="show-high-salary-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>就业时长</h3>
                        <div class="value" id="job-time-count">-</div>
                        <div class="description">每个学生的就业时长</div>
                        <button id="show-job-time-stat-btn">查看详情</button>
                    </div>
                    
                    <div class="stat-card">
                        <h3>班级平均就业时长</h3>
                        <div class="value" id="class-avg-job-time">-</div>
                        <div class="description">每个班级的平均就业时长</div>
                        <button id="show-class-avg-job-time-stat-btn">查看详情</button>
                    </div>
                </div>
                
                <!-- 统计详情展示区域 -->
                <div id="statistics-detail-container" class="card" style="display: none;">
                    <h3>统计详情</h3>
                    <div id="statistics-detail-content">
                        <!-- 统计详情将在这里显示 -->
                    </div>
                </div>
            </div>
        </div>
    </div>
    
    <script>
        // 全局变量
        const API_BASE_URL = 'http://localhost:8000';
        
        // DOM元素加载完成后执行
        document.addEventListener('DOMContentLoaded', function() {
            // 初始化导航标签切换
            initTabNavigation();
            
            // 初始化学生管理功能
            initStudentManagement();
            
            // 初始化成绩管理功能
            initScoreManagement();
            
            // 初始化就业管理功能
            initEmploymentManagement();
            
            // 初始化统计分析功能
            initStatistics();
        });
        
        // 初始化导航标签切换
        function initTabNavigation() {
            const navLinks = document.querySelectorAll('.nav-link');
            const tabContents = document.querySelectorAll('.tab-content');
            
            navLinks.forEach(link => {
                link.addEventListener('click', function(e) {
                    e.preventDefault();
                    
                    // 移除所有活动状态
                    navLinks.forEach(l => l.classList.remove('active'));
                    tabContents.forEach(t => t.classList.remove('active'));
                    
                    // 添加当前活动状态
                    this.classList.add('active');
                    const tabId = this.getAttribute('data-tab');
                    document.getElementById(tabId).classList.add('active');
                });
            });
        }
        
        // 初始化学生管理功能
        function initStudentManagement() {
            // 添加学生按钮点击事件
            document.getElementById('add-student-btn').addEventListener('click', function() {
                const form = document.getElementById('add-student-form');
                form.style.display = form.style.display === 'none' ? 'block' : 'none';
            });
            
            // 显示所有学生按钮点击事件
            document.getElementById('show-all-students-btn').addEventListener('click', function() {
                getAllStudents();
            });
            
            // 搜索学生按钮点击事件
            document.getElementById('search-student-btn').addEventListener('click', function() {
                const studentName = document.getElementById('search-student-name').value;
                if (studentName) {
                    searchStudentByName(studentName);
                } else {
                    showMessage('请输入学生姓名', 'error');
                }
            });
            
            // 学生表单提交事件
            document.getElementById('student-form').addEventListener('submit', function(e) {
                e.preventDefault();
                addStudent();
            });
            
            // 显示所有班级按钮点击事件
            document.getElementById('show-all-classes-btn').addEventListener('click', function() {
                getAllClasses();
            });
            
            // 添加班级按钮点击事件
            document.getElementById('add-class-btn').addEventListener('click', function() {
                const form = document.getElementById('add-class-form');
                form.style.display = form.style.display === 'none' ? 'block' : 'none';
            });
            
            // 班级表单提交事件
            document.getElementById('class-form').addEventListener('submit', function(e) {
                e.preventDefault();
                addClass();
            });
        }
        
        // 初始化成绩管理功能
        function initScoreManagement() {
            // 添加成绩按钮点击事件
            document.getElementById('add-score-btn').addEventListener('click', function() {
                const form = document.getElementById('add-score-form');
                form.style.display = form.style.display === 'none' ? 'block' : 'none';
            });
            
            // 显示所有成绩按钮点击事件
            document.getElementById('show-all-scores-btn').addEventListener('click', function() {
                getAllScores();
            });
            
            // 搜索成绩按钮点击事件
            document.getElementById('search-score-btn').addEventListener('click', function() {
                const studentName = document.getElementById('search-score-name').value;
                if (studentName) {
                    searchScoreByName(studentName);
                } else {
                    showMessage('请输入学生姓名', 'error');
                }
            });
            
            // 成绩表单提交事件
            document.getElementById('score-form').addEventListener('submit', function(e) {
                e.preventDefault();
                addScore();
            });
        }
        
        // 初始化就业管理功能
        function initEmploymentManagement() {
            // 添加就业信息按钮点击事件
            document.getElementById('add-employment-btn').addEventListener('click', function() {
                const form = document.getElementById('add-employment-form');
                form.style.display = form.style.display === 'none' ? 'block' : 'none';
            });
            
            // 显示所有就业信息按钮点击事件
            document.getElementById('show-all-employments-btn').addEventListener('click', function() {
                getAllEmployments();
            });
            
            // 搜索就业信息表单提交事件
            document.getElementById('search-employment-form').addEventListener('submit', function(e) {
                e.preventDefault();
                searchEmployment();
            });
            
            // 就业信息表单提交事件
            document.getElementById('employment-form').addEventListener('submit', function(e) {
                e.preventDefault();
                addEmployment();
            });
        }
        
        // 初始化统计分析功能
        function initStatistics() {
            // 超过30岁的学员统计按钮点击事件
            document.getElementById('show-age-stat-btn').addEventListener('click', function() {
                getStudentsByAge(30);
            });
            
            // 班级性别统计按钮点击事件
            document.getElementById('show-gender-stat-btn').addEventListener('click', function() {
                // 显示输入框让用户输入班级ID
                const classIdDialog = document.createElement('div');
                classIdDialog.id = 'classIdDialogContainer';
                classIdDialog.innerHTML = `
                    <div class="input-dialog">
                        <div class="input-dialog-content">
                            <h3>请输入班级编号</h3>
                            <input type="text" id="classIdInput" placeholder="班级编号">
                            <div class="dialog-buttons">
                                <button id="confirmClassId">确定</button>
                                <button id="cancelClassId">取消</button>
                            </div>
                        </div>
                    </div>
                `;
                document.body.appendChild(classIdDialog);
                
                // 确定按钮点击事件
                document.getElementById('confirmClassId').addEventListener('click', function() {
                    const classId = document.getElementById('classIdInput').value;
                    if (classId) {
                        getGenderCountByClass(classId);
                    }
                    const dialogElement = document.getElementById('classIdDialogContainer');
                    if (dialogElement && dialogElement.parentNode) {
                        document.body.removeChild(dialogElement);
                    }
                });
                
                // 取消按钮点击事件
                document.getElementById('cancelClassId').addEventListener('click', function() {
                    const dialogElement = document.getElementById('classIdDialogContainer');
                    if (dialogElement && dialogElement.parentNode) {
                        document.body.removeChild(dialogElement);
                    }
                });
            });
            
            // 高成绩学生统计按钮点击事件
            document.getElementById('show-high-score-stat-btn').addEventListener('click', function() {
                getHighScoreStudents(80);
            });
            
            // 不及格学生统计按钮点击事件
            document.getElementById('show-fail-stat-btn').addEventListener('click', function() {
                getFailStudents(2);
            });
            
            // 班级平均分统计按钮点击事件
            document.getElementById('show-class-avg-stat-btn').addEventListener('click', function() {
                getClassAvgScore();
            });
            
            // 高薪学生统计按钮点击事件
            document.getElementById('show-high-salary-stat-btn').addEventListener('click', function() {
                getHighSalaryStudents();
            });
            
            // 就业时长统计按钮点击事件
            document.getElementById('show-job-time-stat-btn').addEventListener('click', function() {
                getJobTimeStatistics();
            });
            
            // 班级平均就业时长统计按钮点击事件
            document.getElementById('show-class-avg-job-time-stat-btn').addEventListener('click', function() {
                getClassAvgJobTime();
            });
        }
        
        // API请求函数
        
        // 获取所有学生
        function getAllStudents() {
            fetch(`${API_BASE_URL}/students/`)
                .then(response => {
                    if (!response.ok) {
                        throw new Error(`HTTP error! status: ${response.status}`);
                    }
                    return response.json();
                })
                .then(data => {
                    // 确保data是数组
                    if (Array.isArray(data)) {
                        displayStudents(data);
                    } else {
                        console.error('Expected array but got:', typeof data, data);
                        showMessage('获取学生列表失败：数据格式错误', 'error');
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    // 确保错误消息是字符串
                    let errorMessage = '获取学生列表失败';
                    try {
                        if (error instanceof Error) {
                            errorMessage = `获取学生列表失败: ${error.message}`;
                        } else if (typeof error === 'string') {
                            errorMessage = `获取学生列表失败: ${error}`;
                        } else if (typeof error === 'object' && error !== null) {
                            errorMessage = `获取学生列表失败: ${JSON.stringify(error)}`;
                        } else {
                            errorMessage = `获取学生列表失败: ${String(error)}`;
                        }
                    } catch (e) {
                        console.error('Error processing error message:', e);
                    }
                    showMessage(errorMessage, 'error');
                });
        }
        
        // 按姓名搜索学生
        function searchStudentByName(name) {
            fetch(`${API_BASE_URL}/students/student/${name}`)
                .then(response => response.json())
                .then(data => {
                    displayStudents(Array.isArray(data) ? data : [data]);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('搜索学生失败', 'error');
                });
        }
        
        // 验证班级编号是否存在
        function validateClassId(classId) {
            return fetch(`${API_BASE_URL}/students/class/class_search?class_id=${encodeURIComponent(classId)}`)
                .then(response => {
                    if (response.ok) {
                        return response.json().then(data => {
                            // 检查返回的数据是否为空，如果为空则班级不存在
                            return data !== null && data !== undefined;
                        });
                    } else {
                        return false; // 班级不存在
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    return false; // 发生错误，认为班级不存在
                });
        }
        
        // 添加学生
        function addStudent() {
            const form = document.getElementById('student-form');
            const formData = new FormData(form);
            const classId = formData.get('class_id');
            
            // 先验证班级编号是否存在
            validateClassId(classId).then(classExists => {
                if (!classExists) {
                    showMessage('班级编号不存在，请输入有效的班级编号', 'error');
                    return;
                }
                
                // 转换为JSON对象
                const studentData = {
                    class_id: formData.get('class_id'),
                    student_name: formData.get('student_name'),
                    native_place: formData.get('native_place'),
                    graduate_school: formData.get('graduate_school'),
                    major: formData.get('major'),
                    enrollment_date: formData.get('enrollment_date'),
                    graduation_date: formData.get('graduation_date'),
                    education: formData.get('education'),
                    advisor_id: formData.get('advisor_id'),
                    age: parseInt(formData.get('age')),
                    gender: formData.get('gender')
                };
                
                fetch(`${API_BASE_URL}/students/student/add/`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify(studentData)
                })
                .then(response => response.json())
                .then(data => {
                    if (data.message && data.message.includes('学生已经存在')) {
                        showMessage('学生已存在', 'error');
                    } else {
                        showMessage('学生添加成功', 'success');
                        form.reset();
                        document.getElementById('add-student-form').style.display = 'none';
                        getAllStudents();
                    }
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('添加学生失败', 'error');
                });
            });
        }
        
        // 显示学生列表
        function displayStudents(students) {
            const container = document.getElementById('student-table-container');
            
            if (!students || students.length === 0) {
                container.innerHTML = '<p>没有找到学生信息</p>';
                return;
            }
            
            let tableHtml = `
                <table>
                    <thead>
                        <tr>
                            <th>学生编号</th>
                            <th>班级编号</th>
                            <th>学生姓名</th>
                            <th>籍贯</th>
                            <th>毕业院校</th>
                            <th>专业</th>
                            <th>入学时间</th>
                            <th>毕业时间</th>
                            <th>学历</th>
                            <th>顾问编号</th>
                            <th>年龄</th>
                            <th>性别</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;
            
            students.forEach(student => {
                tableHtml += `
                    <tr>
                        <td>${student.student_id}</td>
                        <td>${student.class_id}</td>
                        <td>${student.student_name}</td>
                        <td>${student.native_place}</td>
                        <td>${student.graduate_school}</td>
                        <td>${student.major}</td>
                        <td>${student.enrollment_date}</td>
                        <td>${student.graduation_date}</td>
                        <td>${student.education}</td>
                        <td>${student.advisor_id}</td>
                        <td>${student.age}</td>
                        <td>${student.gender}</td>
                        <td>
                            <button class="update-student-btn" data-student-id="${student.student_id}">更新</button>
                            <button class="delete-student-btn" data-student-name="${student.student_name}">删除</button>
                        </td>
                    </tr>
                `;
            });
            
            tableHtml += '</tbody></table>';
            container.innerHTML = tableHtml;
            
            // 使用事件委托绑定更新按钮点击事件
            const updateButtons = container.querySelectorAll('.update-student-btn');
            updateButtons.forEach(button => {
                button.addEventListener('click', function() {
                    const studentId = this.getAttribute('data-student-id');
                    updateStudent(studentId);
                });
            });
            
            // 使用事件委托绑定删除按钮点击事件
            const deleteButtons = container.querySelectorAll('.delete-student-btn');
            deleteButtons.forEach(button => {
                button.addEventListener('click', function() {
                    const studentName = this.getAttribute('data-student-name');
                    deleteStudent(studentName);
                });
            });
        }
        
        // 获取所有班级
        function getAllClasses() {
            fetch(`${API_BASE_URL}/students/class/`)
                .then(response => response.json())
                .then(data => {
                    displayClasses(data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取班级列表失败', 'error');
                });
        }
        
        // 显示班级列表
        function displayClasses(classes) {
            const container = document.getElementById('class-table-container');
            
            if (!classes || classes.length === 0) {
                container.innerHTML = '<p>没有找到班级信息</p>';
                return;
            }
            
            let tableHtml = `
                <table>
                    <thead>
                        <tr>
                            <th>班级编号</th>
                            <th>班级名称</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;
            
            classes.forEach(cls => {
                tableHtml += `
                    <tr>
                        <td>${cls.class_id}</td>
                        <td>${cls.class_name}</td>
                        <td>
                            <button onclick="updateClass('${cls.class_id}')">更新</button>
                            <button onclick="deleteClass('${cls.class_id}')">删除</button>
                        </td>
                    </tr>
                `;
            });
            
            tableHtml += '</tbody></table>';
            container.innerHTML = tableHtml;
        }
        
        // 添加班级
        function addClass() {
            const form = document.getElementById('class-form');
            const formData = new FormData(form);
            
            // 验证班级编号格式
            const classId = formData.get('class_id');
            if (classId.length !== 6) {
                showMessage('班级编号必须是6个字符', 'error');
                return;
            }
            
            // 转换为JSON对象
            const classData = {
                class_id: classId,
                class_name: formData.get('class_name')
            };
            
            fetch(`${API_BASE_URL}/students/class/add/`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(classData)
            })
            .then(response => response.json())
            .then(data => {
                if (data.message && data.message.includes('班级已经存在')) {
                    showMessage('班级已存在', 'error');
                } else {
                    showMessage('班级添加成功', 'success');
                    form.reset();
                    document.getElementById('add-class-form').style.display = 'none';
                    getAllClasses();
                }
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('添加班级失败', 'error');
            });
        }
        
        // 获取所有成绩
        function getAllScores() {
            fetch(`${API_BASE_URL}/score/score/all`)
                .then(response => response.json())
                .then(data => {
                    displayScores(data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取成绩列表失败', 'error');
                });
        }
        
        // 按姓名搜索成绩
        function searchScoreByName(name) {
            fetch(`${API_BASE_URL}/score/score/search_by_name?stu_name=${encodeURIComponent(name)}`)
                .then(response => response.json())
                .then(data => {
                    displayScores(Array.isArray(data) ? data : [data]);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('搜索成绩失败', 'error');
                });
        }
        
        // 添加成绩
        function addScore() {
            const form = document.getElementById('score-form');
            const formData = new FormData(form);
            
            // 转换为JSON对象
            const scoreData = {
                student_id: parseInt(formData.get('student_id')),
                round: parseInt(formData.get('round')),
                score: parseInt(formData.get('score'))
            };
            
            fetch(`${API_BASE_URL}/score/score/add`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(scoreData)
            })
            .then(response => response.json())
            .then(data => {
                showMessage('成绩添加成功', 'success');
                form.reset();
                document.getElementById('add-score-form').style.display = 'none';
                getAllScores();
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('添加成绩失败', 'error');
            });
        }
        
        // 显示成绩列表
        function displayScores(scores) {
            const container = document.getElementById('score-table-container');
            
            if (!scores || scores.length === 0) {
                container.innerHTML = '<p>没有找到成绩信息</p>';
                return;
            }
            
            let tableHtml = `
                <table>
                    <thead>
                        <tr>
                            <th>成绩ID</th>
                            <th>学生编号</th>
                            <th>考核序次</th>
                            <th>成绩</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;
            
            scores.forEach(score => {
                tableHtml += `
                    <tr>
                        <td>${score.score_id}</td>
                        <td>${score.student_id}</td>
                        <td>${score.round}</td>
                        <td>${score.score}</td>
                        <td>
                            <button onclick="updateScore('${score.score_id}')">更新</button>
                            <button onclick="deleteScore('${score.student_id}', '${score.round}')">删除</button>
                        </td>
                    </tr>
                `;
            });
            
            tableHtml += '</tbody></table>';
            container.innerHTML = tableHtml;
        }
        
        // 获取所有就业信息
        function getAllEmployments() {
            fetch(`${API_BASE_URL}/employment/employment/all`)
                .then(response => {
                    console.log('获取所有就业信息响应状态:', response.status);
                    return response.json();
                })
                .then(data => {
                    console.log('所有就业信息:', data);
                    // 处理后端返回的字符串响应
                    if (typeof data === 'string') {
                        document.getElementById('employment-table-container').innerHTML = '<p>' + data + '</p>';
                        return;
                    }
                    // 确保数据是数组格式
                    const employments = Array.isArray(data) ? data : (data ? [data] : []);
                    displayEmployments(employments);
                })
                .catch(error => {
                    console.error('获取所有就业信息错误:', error);
                    showMessage('获取就业信息列表失败', 'error');
                });
        }
        
        // 搜索就业信息
        function searchEmployment() {
            const form = document.getElementById('search-employment-form');
            const formData = new FormData(form);
            
            // 构建请求体，参数名与后端期望的一致
            const requestBody = {};
            if (formData.get('stu_id')) requestBody.stu_id = parseInt(formData.get('stu_id'));
            if (formData.get('comp_name')) requestBody.cp_name = formData.get('comp_name');
            if (formData.get('min_sal')) requestBody.min_sal = parseInt(formData.get('min_sal'));
            if (formData.get('max_sal')) requestBody.max_sal = parseInt(formData.get('max_sal'));
            
            console.log('搜索参数:', requestBody);
            
            fetch(`${API_BASE_URL}/employment/employment/search`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(requestBody)
            })
                .then(response => {
                    console.log('响应状态:', response.status);
                    console.log('响应头:', response.headers);
                    return response.text().then(text => {
                        console.log('响应原始文本:', text);
                        try {
                            return JSON.parse(text);
                        } catch (e) {
                            console.error('解析JSON失败:', e);
                            return text;
                        }
                    });
                })
                .then(data => {
                    console.log('搜索结果:', data);
                    // 处理后端返回的字符串响应
                    if (typeof data === 'string') {
                        if (data === '未找到匹配的就业信息' || data === '请输入查询内容!') {
                            document.getElementById('employment-table-container').innerHTML = '<p>' + data + '</p>';
                            return;
                        }
                    }
                    // 确保数据是数组格式
                    const employments = Array.isArray(data) ? data : (data ? [data] : []);
                    displayEmployments(employments);
                })
                .catch(error => {
                    console.error('搜索错误:', error);
                    showMessage('搜索就业信息失败', 'error');
                });
        }
        
        // 添加就业信息
        function addEmployment() {
            const form = document.getElementById('employment-form');
            const formData = new FormData(form);
            
            // 转换为JSON对象
            const employmentData = {
                student_id: parseInt(formData.get('student_id')),
                open_time: formData.get('open_time'),
                offer_time: formData.get('offer_time'),
                company: formData.get('company'),
                salary: parseInt(formData.get('salary'))
            };
            
            fetch(`${API_BASE_URL}/employment/employment/add`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(employmentData)
            })
            .then(response => response.json())
            .then(data => {
                showMessage('就业信息添加成功', 'success');
                form.reset();
                document.getElementById('add-employment-form').style.display = 'none';
                getAllEmployments();
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('添加就业信息失败', 'error');
            });
        }
        
        // 显示就业信息列表
        function displayEmployments(employments) {
            const container = document.getElementById('employment-table-container');
            
            if (!employments || employments.length === 0) {
                container.innerHTML = '<p>没有找到就业信息</p>';
                return;
            }
            
            let tableHtml = `
                <table>
                    <thead>
                        <tr>
                            <th>就业ID</th>
                            <th>学生编号</th>
                            <th>就业开放时间</th>
                            <th>Offer下发时间</th>
                            <th>公司名称</th>
                            <th>就业薪资</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
            `;
            
            employments.forEach(emp => {
                // 确保每个字段都有值，如果没有则显示为空字符串
                const empId = emp.emp_id || '';
                const studentId = emp.student_id || '';
                const openTime = emp.open_time || '';
                const offerTime = emp.offer_time || '';
                const company = emp.company || '';
                const salary = emp.salary || '';
                
                tableHtml += `
                    <tr>
                        <td>${empId}</td>
                        <td>${studentId}</td>
                        <td>${openTime}</td>
                        <td>${offerTime}</td>
                        <td>${company}</td>
                        <td>${salary}</td>
                        <td>
                            <button onclick="updateEmployment('${empId}')">更新</button>
                            <button onclick="deleteEmployment('${studentId}')">删除</button>
                        </td>
                    </tr>
                `;
            });
            
            tableHtml += '</tbody></table>';
            container.innerHTML = tableHtml;
        }
        
        // 获取超过指定年龄的学生
        function getStudentsByAge(age) {
            fetch(`${API_BASE_URL}/statistics/statistics/stu_info_age?age=${age}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('age-count').textContent = data.length;
                    displayStatisticsDetail('超过' + age + '岁的学员', data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取年龄统计失败', 'error');
                });
        }
        
        // 获取班级性别统计
        function getGenderCountByClass(classId) {
            fetch(`${API_BASE_URL}/statistics/statistics/count_gender?cid=${classId}`)
                .then(response => response.json())
                .then(data => {
                    console.log('性别统计数据:', data);
                    // 处理后端返回的数组格式数据
                    let maleCount = 0;
                    let femaleCount = 0;
                    
                    if (Array.isArray(data)) {
                        data.forEach(item => {
                            if (item.gender === '男') {
                                maleCount = item.count;
                            } else if (item.gender === '女') {
                                femaleCount = item.count;
                            }
                        });
                    }
                    
                    document.getElementById('gender-count').textContent = maleCount + femaleCount;
                    displayStatisticsDetail('班级 ' + classId + ' 性别统计', [
                        { '统计项': '男生人数', '数量': maleCount },
                        { '统计项': '女生人数', '数量': femaleCount }
                    ]);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取性别统计失败', 'error');
                });
        }
        
        // 获取高成绩学生
        function getHighScoreStudents(score) {
            fetch(`${API_BASE_URL}/statistics/statistics/stu_score?sc=${score}`)
                .then(response => response.json())
                .then(data => {
                    document.getElementById('high-score-count').textContent = data.length;
                    displayStatisticsDetail('每次考试成绩都在' + score + '分以上的学生', data);
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取高成绩学生统计失败', 'error');
                });
        }
        
        // 获取不及格学生
        function getFailStudents(count) {
            fetch(`${API_BASE_URL}/statistics/statistics/grade_fail`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ ct: count })
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('fail-count').textContent = data.length;
                displayStatisticsDetail('有' + count + '次以上不及格的学生', data);
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('获取不及格学生统计失败', 'error');
            });
        }
        
        // 获取班级平均分
        function getClassAvgScore() {
            fetch(`${API_BASE_URL}/statistics/statistics/grade_avg_by_class`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('class-avg-score').textContent = data.length;
                displayStatisticsDetail('每次考试每个班级的平均分', data);
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('获取班级平均分统计失败', 'error');
            });
        }
        
        // 获取高薪学生
        function getHighSalaryStudents() {
            fetch(`${API_BASE_URL}/statistics/statistics/stu_5limit_sal`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('high-salary-count').textContent = data.length;
                displayStatisticsDetail('就业薪资最高的前五名学生', data);
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('获取高薪学生统计失败', 'error');
            });
        }
        
        // 获取就业时长统计
        function getJobTimeStatistics() {
            fetch(`${API_BASE_URL}/statistics/statistics/stu_find_job_time`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('job-time-count').textContent = data.length;
                displayStatisticsDetail('每个学生的就业时长', data);
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('获取就业时长统计失败', 'error');
            });
        }
        
        // 获取班级平均就业时长
        function getClassAvgJobTime() {
            fetch(`${API_BASE_URL}/statistics/statistics/stu_avgtime_find_job`, {
                method: 'POST'
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('class-avg-job-time').textContent = data.length;
                displayStatisticsDetail('每个班级的平均就业时长', data);
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('获取班级平均就业时长统计失败', 'error');
            });
        }
        
        // 显示统计详情
        function displayStatisticsDetail(title, data) {
            const container = document.getElementById('statistics-detail-container');
            const content = document.getElementById('statistics-detail-content');
            
            container.style.display = 'block';
            
            let html = `<h4>${title}</h4>`;
            
            if (Array.isArray(data) && data.length > 0) {
                // 获取所有可能的键
                const keys = Object.keys(data[0]);
                
                html += '<table><thead><tr>';
                keys.forEach(key => {
                    html += `<th>${key}</th>`;
                });
                html += '</tr></thead><tbody>';
                
                data.forEach(item => {
                    html += '<tr>';
                    keys.forEach(key => {
                        html += `<td>${item[key]}</td>`;
                    });
                    html += '</tr>';
                });
                
                html += '</tbody></table>';
            } else {
                html += '<p>没有找到相关数据</p>';
            }
            
            content.innerHTML = html;
        }
        
        // 显示消息
        function showMessage(message, type = 'info') {
            const container = document.getElementById('message-container');
            const messageElement = document.createElement('div');
            messageElement.className = `message ${type}`;
            messageElement.textContent = message;
            
            container.innerHTML = '';
            container.appendChild(messageElement);
            
            // 3秒后自动消失
            setTimeout(() => {
                messageElement.remove();
            }, 3000);
        }
        
        // 通用输入对话框函数
        function showInputDialog(title, placeholder, callback) {
            // 创建输入对话框
            const dialog = document.createElement('div');
            dialog.id = 'customInputDialog';
            dialog.className = 'input-dialog';
            dialog.innerHTML = `
                <div class="input-dialog-content">
                    <h3>${title}</h3>
                    <input type="text" id="dialogInput" placeholder="${placeholder}">
                    <div class="dialog-buttons">
                        <button id="confirmDialog">确定</button>
                        <button id="cancelDialog">取消</button>
                    </div>
                </div>
            `;
            document.body.appendChild(dialog);
            
            // 确定按钮点击事件
            document.getElementById('confirmDialog').addEventListener('click', function() {
                const value = document.getElementById('dialogInput').value;
                const dialogElement = document.getElementById('customInputDialog');
                if (dialogElement && dialogElement.parentNode) {
                    dialogElement.parentNode.removeChild(dialogElement);
                }
                callback(value);
            });
            
            // 取消按钮点击事件
            document.getElementById('cancelDialog').addEventListener('click', function() {
                const dialogElement = document.getElementById('customInputDialog');
                if (dialogElement && dialogElement.parentNode) {
                    dialogElement.parentNode.removeChild(dialogElement);
                }
                callback(null);
            });
            
            // 输入框回车事件
            document.getElementById('dialogInput').addEventListener('keypress', function(e) {
                if (e.key === 'Enter') {
                    document.getElementById('confirmDialog').click();
                }
            });
        }
        
        // 更新学生信息
        function updateStudent(studentId) {
            // 获取所有学生信息，然后根据ID查找特定学生
            fetch(`${API_BASE_URL}/students/`)
                .then(response => response.json())
                .then(data => {
                    // 处理API返回的数据
                    let students = data;
                    if (!Array.isArray(data)) {
                        students = data ? [data] : [];
                    }
                    
                    // 根据ID查找学生
                    const student = students.find(s => s.student_id == studentId);
                    
                    // 检查student对象是否存在
                    if (!student) {
                        showMessage('未找到学生信息', 'error');
                        return;
                    }
                    
                    // 创建一个表单对话框，允许用户编辑所有学生信息
                    const dialog = document.createElement('div');
                    dialog.id = 'updateStudentDialog';
                    dialog.className = 'input-dialog';
                    dialog.innerHTML = `
                        <div class="input-dialog-content" style="width: 500px; max-height: 80vh; overflow-y: auto;">
                            <h3>更新学生信息</h3>
                            <div class="form-group">
                                <label>学生编号</label>
                                <input type="text" id="update_student_id" value="${student.student_id || ''}" readonly>
                            </div>
                            <div class="form-group">
                                <label>班级编号</label>
                                <input type="text" id="update_class_id" value="${student.class_id || ''}" readonly>
                            </div>
                            <div class="form-group">
                                <label>学生姓名</label>
                                <input type="text" id="update_student_name" value="${student.student_name || ''}">
                            </div>
                            <div class="form-group">
                                <label>籍贯</label>
                                <input type="text" id="update_native_place" value="${student.native_place || ''}">
                            </div>
                            <div class="form-group">
                                <label>毕业院校</label>
                                <input type="text" id="update_graduate_school" value="${student.graduate_school || ''}">
                            </div>
                            <div class="form-group">
                                <label>专业</label>
                                <input type="text" id="update_major" value="${student.major || ''}">
                            </div>
                            <div class="form-group">
                                <label>入学时间</label>
                                <input type="date" id="update_enrollment_date" value="${student.enrollment_date || ''}">
                            </div>
                            <div class="form-group">
                                <label>毕业时间</label>
                                <input type="date" id="update_graduation_date" value="${student.graduation_date || ''}">
                            </div>
                            <div class="form-group">
                                <label>学历</label>
                                <select id="update_education">
                                    <option value="本科" ${student.education === '本科' ? 'selected' : ''}>本科</option>
                                    <option value="专科" ${student.education === '专科' ? 'selected' : ''}>专科</option>
                                    <option value="硕士" ${student.education === '硕士' ? 'selected' : ''}>硕士</option>
                                    <option value="博士" ${student.education === '博士' ? 'selected' : ''}>博士</option>
                                </select>
                            </div>
                            <div class="form-group">
                                <label>顾问编号</label>
                                <input type="text" id="update_advisor_id" value="${student.advisor_id || ''}">
                            </div>
                            <div class="form-group">
                                <label>年龄</label>
                                <input type="number" id="update_age" value="${student.age || 0}" min="0" max="100">
                            </div>
                            <div class="form-group">
                                <label>性别</label>
                                <select id="update_gender">
                                    <option value="男" ${student.gender === '男' ? 'selected' : ''}>男</option>
                                    <option value="女" ${student.gender === '女' ? 'selected' : ''}>女</option>
                                </select>
                            </div>
                            <div class="dialog-buttons">
                                <button id="confirmUpdate">确定</button>
                                <button id="cancelUpdate">取消</button>
                            </div>
                        </div>
                    `;
                    document.body.appendChild(dialog);
                    
                    // 确定按钮点击事件
                    document.getElementById('confirmUpdate').addEventListener('click', function() {
                        // 获取表单数据
                        const updatedStudent = {
                            student_id: parseInt(document.getElementById('update_student_id').value),
                            class_id: document.getElementById('update_class_id').value,
                            student_name: document.getElementById('update_student_name').value,
                            native_place: document.getElementById('update_native_place').value,
                            graduate_school: document.getElementById('update_graduate_school').value,
                            major: document.getElementById('update_major').value,
                            enrollment_date: document.getElementById('update_enrollment_date').value,
                            graduation_date: document.getElementById('update_graduation_date').value,
                            education: document.getElementById('update_education').value,
                            advisor_id: document.getElementById('update_advisor_id').value,
                            age: parseInt(document.getElementById('update_age').value),
                            gender: document.getElementById('update_gender').value
                        };
                        
                        // 发送更新请求
                        fetch(`${API_BASE_URL}/students/student/update/`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(updatedStudent)
                        })
                        .then(response => response.json())
                        .then(data => {
                            showMessage('学生信息更新成功', 'success');
                            getAllStudents();
                            // 关闭对话框
                            const dialogElement = document.getElementById('updateStudentDialog');
                            if (dialogElement && dialogElement.parentNode) {
                                dialogElement.parentNode.removeChild(dialogElement);
                            }
                        })
                        .catch(error => {
                            console.error('Error:', error);
                            showMessage('更新学生信息失败', 'error');
                        });
                    });
                    
                    // 取消按钮点击事件
                    document.getElementById('cancelUpdate').addEventListener('click', function() {
                        const dialogElement = document.getElementById('updateStudentDialog');
                        if (dialogElement && dialogElement.parentNode) {
                            dialogElement.parentNode.removeChild(dialogElement);
                        }
                    });
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('获取学生信息失败', 'error');
                });
        }
        
        // 删除学生
        function deleteStudent(studentName) {
            if (!confirm('确定要删除该学生吗？')) return;
            
            // 将stu_name作为查询参数发送
            fetch(`${API_BASE_URL}/students/student/delete/?stu_name=${encodeURIComponent(studentName)}`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                }
            })
            .then(response => {
                if (!response.ok) {
                    // 尝试解析JSON响应，如果失败则使用状态文本
                    return response.json().catch(() => {
                        throw new Error(response.statusText || '删除失败');
                    }).then(errData => {
                        // 确保errData.detail是字符串
                        let errorMessage = '删除失败';
                        if (errData && typeof errData.detail === 'string') {
                            errorMessage = errData.detail;
                        } else if (errData && typeof errData.detail === 'object') {
                            errorMessage = JSON.stringify(errData.detail);
                        } else if (errData && errData.detail) {
                            errorMessage = String(errData.detail);
                        }
                        throw new Error(errorMessage);
                    });
                }
                return response.json();
            })
            .then(data => {
                showMessage('学生删除成功', 'success');
                getAllStudents();
            })
            .catch(error => {
                console.error('Error:', error);
                // 确保错误消息是字符串
                let errorMessage = '删除学生失败';
                try {
                    // 检查error是否是Error对象
                    if (error instanceof Error) {
                        errorMessage = `删除学生失败: ${error.message}`;
                    } 
                    // 检查error是否是字符串
                    else if (typeof error === 'string') {
                        errorMessage = `删除学生失败: ${error}`;
                    }
                    // 检查error是否是对象
                    else if (typeof error === 'object' && error !== null) {
                        // 尝试获取message属性
                        if (error.message) {
                            if (typeof error.message === 'string') {
                                errorMessage = `删除学生失败: ${error.message}`;
                            } else {
                                errorMessage = `删除学生失败: ${JSON.stringify(error.message)}`;
                            }
                        }
                        // 如果没有message属性，尝试获取detail属性
                        else if (error.detail) {
                            if (typeof error.detail === 'string') {
                                errorMessage = `删除学生失败: ${error.detail}`;
                            } else {
                                errorMessage = `删除学生失败: ${JSON.stringify(error.detail)}`;
                            }
                        }
                        // 如果都没有，将整个error对象转换为字符串
                        else {
                            errorMessage = `删除学生失败: ${JSON.stringify(error)}`;
                        }
                    }
                    // 其他情况，直接转换为字符串
                    else {
                        errorMessage = `删除学生失败: ${String(error)}`;
                    }
                } catch (e) {
                    console.error('Error processing error message:', e);
                }
                // 最终确保errorMessage是字符串
                if (typeof errorMessage !== 'string') {
                    errorMessage = String(errorMessage);
                }
                showMessage(errorMessage, 'error');
            });
        }
        
        // 更新班级信息
        function updateClass(classId) {
            // 使用自定义输入对话框替代prompt
            showInputDialog('请输入新的班级名称', '班级名称', function(newName) {
                if (!newName) return;
                
                // 获取班级当前信息
                fetch(`${API_BASE_URL}/students/class/class_search?class_id=${classId}`)
                    .then(response => response.json())
                    .then(cls => {
                        // 更新班级名称
                        cls.class_name = newName;
                        
                        // 发送更新请求
                        fetch(`${API_BASE_URL}/students/class/add/`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(cls)
                        })
                        .then(response => response.json())
                        .then(data => {
                            showMessage('班级信息更新成功', 'success');
                            getAllClasses();
                        })
                        .catch(error => {
                            console.error('Error:', error);
                            showMessage('更新班级信息失败', 'error');
                        });
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showMessage('获取班级信息失败', 'error');
                    });
            });
        }
        
        // 删除班级
        function deleteClass(classId) {
            if (!confirm('确定要删除该班级吗？')) return;
            
            // 注意：后端API中没有提供删除班级的接口，这里只是示例
            showMessage('删除班级功能尚未实现', 'info');
        }
        
        // 更新成绩
        function updateScore(scoreId) {
            // 使用自定义输入对话框替代prompt
            showInputDialog('请输入新的成绩', '成绩', function(newScore) {
                if (!newScore || isNaN(newScore)) return;
                
                // 获取成绩当前信息
                fetch(`${API_BASE_URL}/score/score/all`)
                    .then(response => response.json())
                    .then(scores => {
                        const score = scores.find(s => s.score_id == scoreId);
                        if (!score) {
                            showMessage('找不到该成绩记录', 'error');
                            return;
                        }
                        
                        // 更新成绩
                        score.score = parseInt(newScore);
                        
                        // 发送更新请求
                        fetch(`${API_BASE_URL}/score/score/update`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(score)
                        })
                        .then(response => response.json())
                        .then(data => {
                            showMessage('成绩更新成功', 'success');
                            getAllScores();
                        })
                        .catch(error => {
                            console.error('Error:', error);
                            showMessage('更新成绩失败', 'error');
                        });
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showMessage('获取成绩信息失败', 'error');
                    });
            });
        }
        
        // 删除成绩
        function deleteScore(studentId, round) {
            if (!confirm('确定要删除该成绩吗？')) return;
            
            fetch(`${API_BASE_URL}/score/score/delete?stu_id=${studentId}&rnd=${round}`)
                .then(response => response.json())
                .then(data => {
                    showMessage('成绩删除成功', 'success');
                    getAllScores();
                })
                .catch(error => {
                    console.error('Error:', error);
                    showMessage('删除成绩失败', 'error');
                });
        }
        
        // 更新就业信息
        function updateEmployment(empId) {
            // 使用自定义输入对话框替代prompt
            showInputDialog('请输入新的薪资', '薪资', function(newSalary) {
                if (!newSalary || isNaN(newSalary)) return;
                
                // 获取就业信息当前数据
                fetch(`${API_BASE_URL}/employment/employment/all`)
                    .then(response => response.json())
                    .then(employments => {
                        const emp = employments.find(e => e.emp_id == empId);
                        if (!emp) {
                            showMessage('找不到该就业信息', 'error');
                            return;
                        }
                        
                        // 更新薪资
                        emp.salary = parseInt(newSalary);
                        
                        // 发送更新请求
                        fetch(`${API_BASE_URL}/employment/employment/update`, {
                            method: 'POST',
                            headers: {
                                'Content-Type': 'application/json',
                            },
                            body: JSON.stringify(emp)
                        })
                        .then(response => response.json())
                        .then(data => {
                            showMessage('就业信息更新成功', 'success');
                            getAllEmployments();
                        })
                        .catch(error => {
                            console.error('Error:', error);
                            showMessage('更新就业信息失败', 'error');
                        });
                    })
                    .catch(error => {
                        console.error('Error:', error);
                        showMessage('获取就业信息失败', 'error');
                    });
            });
        }
        
        // 删除就业信息
        function deleteEmployment(studentId) {
            if (!confirm('确定要删除该就业信息吗？')) return;
            
            fetch(`${API_BASE_URL}/employment/employment/delete`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ stu_id: parseInt(studentId) })
            })
            .then(response => response.json())
            .then(data => {
                showMessage('就业信息删除成功', 'success');
                getAllEmployments();
            })
            .catch(error => {
                console.error('Error:', error);
                showMessage('删除就业信息失败', 'error');
            });
        }
    </script>
</body>
</html>